package com.fortune.controller;

import com.alibaba.druid.sql.visitor.functions.Substring;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.bootup.AbstractController;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.dao.CardDAO;
import com.fortune.dal.dao.CreditCardPayRecordDAO;
import com.fortune.dal.dao.CreditCardTypeDAO;
import com.fortune.dal.model.Bank;
import com.fortune.dal.model.Card;
import com.fortune.dal.model.CreditCardPayRecord;
import com.fortune.dal.model.CreditCardType;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("creditcardpayrecord")
public class CreditCardPayRecordController extends AbstractController{

    @Autowired
    private CreditCardPayRecordDAO creditCardPayRecordDAO;

    @Autowired
    private CardDAO cardDAO;

    @Autowired
    private BankDAO bankDAO;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("creditcardpayrecord");
        mv.addObject("menuFlag", "creditcardpayrecord");
        return mv;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> list(@ModelAttribute CreditCardPayRecord creditCardPayRecord) throws BusinessException {
        List<Map<String,Object>> list = Lists.newArrayList() ;
        try {
            List<CreditCardPayRecord> creditCardPayRecords = creditCardPayRecordDAO.queryByModel(creditCardPayRecord) ;
            if(ListUtils.isNotEmpty(creditCardPayRecords)) {
                for(CreditCardPayRecord cardPayRecord:creditCardPayRecords){
                    Map<String,Object> dataMap = MapUtils.parseMap(cardPayRecord) ;
                     Card card = cardDAO.queryById(cardPayRecord.getCardId()) ;
  //                 dataMap.put("bankName",bank.getName()) ;
                    String code = card.getCode();
                    String newCode = code.substring(code.length()-4);

                    Bank bank = bankDAO.queryById(card.getBankId()) ;
                    String allName =bank.getName();
                    String simpleName = allName.substring(0,2);
                   dataMap.put("nameCode",simpleName.concat(newCode)) ;
                    list.add(dataMap) ;
                }
            }
        }catch (Exception ex) {
            logger.error("",ex);
            return Lists.newArrayList() ;
        }
        return list;
    }

}
